package com.celesea.framework.util;

import com.celesea.framework.dao.Filter;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @param <T>
 * @author Administrator
 */
public class PageData<T> {
    /**
     * 结果集
     */
    private List<T> dataList;
    /**
     * 分页
     */
    private Pager pager;
    /**
     * 过滤条件
     */
    private Filter filter;
    /**
     * 排序方式
     */
    private Sort sort;

    private Map<Object, Object> extend = Maps.newHashMap();

    public <C> PageData<C> convert(Function<? super T, ? extends C> mapper) {
        PageData<C> response = new PageData<C>();
        response.setFilter(this.getFilter());
        response.setPager(this.getPager());
        response.setSort(this.getSort());
        response.setExtend(this.getExtend());
        response.setDataList(this.getDataList().stream().map(mapper)
                .collect(Collectors.toList()));
        return response;
    }

    public void append(Object key, Object value) {
        this.extend.put(key, value);
    }

    public List<T> getDataList() {
        return dataList;
    }

    public void setDataList(List<T> dataList) {
        this.dataList = dataList;
    }

    public Pager getPager() {
        return pager;
    }

    public void setPager(Pager pager) {
        this.pager = pager;
    }

    public Filter getFilter() {
        return filter;
    }

    public void setFilter(Filter filter) {
        this.filter = filter;
    }

    public Sort getSort() {
        return sort;
    }

    public void setSort(Sort sort) {
        this.sort = sort;
    }

    public Map<Object, Object> getExtend() {
        return extend;
    }

    public void setExtend(Map<Object, Object> extend) {
        this.extend = extend;
    }
}
